﻿using System;
using System.Html;
using Quicklight.Web.Html;
using jQueryApi;

namespace Quicklight.Web.Razor {

   /// <summary>
   /// Does the binding on behalf of a declarative binding of a ObservableList to an attribute on an element
   /// For example <input type="text" @Binding("value", ViewModel.Items) />
   /// </summary>
   /// <typeparam name="T">Type items in the ObservableList</typeparam>
   public class ObservableListAttributeBinder<T> : AttributeBinder {
      private ObservableList<T> list;

      /// <summary>
      /// Constructs a ObservableListAttributeBinder for binding the ObservableList to the attribute 
      /// </summary>
      /// <param name="attributeName">Name of attribute to bind to</param>
      /// <param name="list">ObservableList to bind to the attribute</param>
      public ObservableListAttributeBinder(string attributeName, ObservableList<T> list)
         : base(attributeName) {

         this.list = list;
      }

      /// <summary>
      /// Bind method does the actual binding of the ObservableList to the attribute of the specified element
      /// </summary>
      /// <param name="element">Element with which the binding must occur</param>
      protected override void Bind(Element element) {
         element.Bind<T>(attributeName, list);

         jQueryObject jQueryObject = jQuery.FromElement(element);
         jQueryObject.Attribute(attributeName, list.ToString());
      }
   }
}
